package com.teach.service.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teach.dto.NewsSearchDTO;
import com.teach.entity.News;
import com.teach.entity.NewsClass;
import com.teach.mapper.NewsClassMapper;
import com.teach.mapper.NewsMapper;
import com.teach.service.NewsService;
import static com.teach.commons.MyBatisUtil.*;

public class NewsServiceImpl implements NewsService {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());


	
	@Override
	public void add(News news) {
		try {
			NewsMapper mapper = getMapper(NewsMapper.class);
			news.setNewsId(UUID.randomUUID().toString());
			news.setNewsTime(new Date());
			mapper.insertSelective(news);
			commit();
//			logger.info("添加新闻成功:"+JSON.toJSONString(news));
			logger.info("添加新闻成功:{}",JSON.toJSONString(news));
		} catch (Exception e) {
			logger.warn("添加失败",e);
			throw new RuntimeException("添加失败", e);
		}
	}

	@Override
	public void modify(News news) {
		try {
			NewsMapper mapper = getMapper(NewsMapper.class);
			mapper.updateByPrimaryKeySelective(news);
			commit();
		} catch (Exception e) {
			throw new RuntimeException("修改失败", e);
		}finally {
			close();
		}
	}

	@Override
	public void delete(String newsId) {
		try {
			NewsMapper mapper = getMapper(NewsMapper.class);
			mapper.deleteByPrimaryKey(newsId);
			commit();
		} catch (Exception e) {
			throw new RuntimeException("删除失败", e);
		}finally {
			close();
		}
		
	}

	@Override
	public News get(String newsId) {
		try {
			NewsMapper mapper = getMapper(NewsMapper.class);
			System.out.println("查询成功");
			return mapper.selectByPrimaryKey(newsId);
		} catch (Exception e) {
			throw new RuntimeException("查询失败", e);
		}finally {
			close();
		}
	}

	@Override
	public PageInfo<News> page(int current, int rowMax) {
		try {
			NewsMapper mapper = getMapper(NewsMapper.class);
			return PageHelper.startPage(current, rowMax).doSelectPageInfo(()->mapper.select());
		} catch (Exception e) {
			logger.warn("分页查询失败", e);
			throw new RuntimeException("分页查询失败", e);
		}finally {
			close();
		}
	}

	@Override
	public PageInfo<News> page(NewsSearchDTO dto) {
		try {
			NewsMapper mapper = getMapper(NewsMapper.class);
			return PageHelper.startPage(dto.getCurrent(), dto.getRowMax()).doSelectPageInfo(()->mapper.selectMore(dto));
		} catch (Exception e) {
			logger.warn("分页查询失败", e);
			throw new RuntimeException("分页查询失败", e);
		}finally {
			close();
		}
	}

	@Override
	public List<NewsClass> listClass() {
		try {
			NewsClassMapper mapper = getMapper(NewsClassMapper.class);
			return mapper.listAll();
		} catch (Exception e) {
			logger.warn("查询失败", e);
			throw new RuntimeException("查询失败", e);
		}finally {
			close();
		}
	}

}
